package cease.http;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.Header;

/**
 * @author dhf
 */
public class HttpRequestResult {
    private byte[] content;

    private List<String> lines = new LinkedList<String>();

    private String charset = "UTF-8";

    private String contentStr;

    private int status;

    private Header[] headers;

    public HttpRequestResult(byte[] content, int status, Header[] headers) {
        this(content, "UTF-8", status, headers);
    }

    public HttpRequestResult(byte[] content, String charset, int status,
            Header[] headers) {
        this.content = content;
        if (null != charset) {
            this.charset = charset;
        }
        if (null != content) {
            // default charset
            try {
                contentStr = new String(content, this.charset);
            } catch (UnsupportedEncodingException e) {
                contentStr = new String(content);
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new ByteArrayInputStream(content)));
            String line = null;
            try {
                while (null != (line = br.readLine())) {
                    lines.add(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        this.status = status;
        this.headers = headers;
    }

    /**
     * @return the content
     */
    public byte[] getContent() {
        return this.content;
    }

    /**
     * @return the contentStr
     */
    public String getContentStr() {
        return this.contentStr;
    }

    /**
     * @return the headers
     */
    public Header[] getHeaders() {
        return this.headers;
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return this.status;
    }

    /**
     * @return the lines
     */
    public List<String> getLines() {
        return this.lines;
    }
}
